﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Configuration;
using System.Windows.Forms;
using System.ComponentModel;

namespace MetMax
{
    [Serializable()]
    [DefaultPropertyAttribute("Name")]
    public class Options : ICloneable //ApplicationSettingsBase, ICloneable //, IXmlSerializable
    {
        #region constructors
        public Options()
        {
            this.wipeTrailingDecimals = false;
            this.wrapArounds = new WrapArounds();
            this.Importer_version = ImporterVersion.RetentionAnalysis;
            this.ColumnDeadTime = 1000f;
            this.secondDimensionLength = 4000f;
            this.exportFact = DisplayProperties.area;
            this.dataset = new MetMaxDataSet();
        }

        public Options(Options other)
            : this()
        {
            this.wipeTrailingDecimals = other.wipeTrailingDecimals;
            this.Importer_version = other.Importer_version;
            this.ColumnDeadTime = other.ColumnDeadTime;
            this.secondDimensionLength = other.secondDimensionLength;
            this.exportFact = other.exportFact;

            this.wrapArounds = new WrapArounds (other.wrapArounds);

            if (other.dataset == null)
            {
                this.dataset = new MetMaxDataSet();
            }
            else
            {
                this.dataset = other.dataset.Copy() as MetMaxDataSet;
            }
        }
        #endregion constructors

        [CategoryAttribute("Misc"),
        DescriptionAttribute("should trailing decimals folowing the metabolite names be removed")]
        public bool wipeTrailingDecimals { get; set; }

        #region version somethings
        public enum ImporterVersion : short
        {
            /// <summary>
            /// haha
            /// </summary>
            RetentionAnalysis = 2,
            /// <summary>
            /// laber
            /// </summary>
            IsotopeConcentrator = 3,
            /// <summary>
            /// lulu
            /// </summary>
            LibrarySearch = 4
        }

        [CategoryAttribute("Version"),
        DescriptionAttribute("the purpose of the current run")]
        public ImporterVersion Importer_version { get; set; }

        [CategoryAttribute("Version"),
        DescriptionAttribute("analysing the retention properties of metabolites accross different samples")]
        public bool version_RetentionAnalysis
        {
            get { return Importer_version == ImporterVersion.RetentionAnalysis; }
            set
            {
                if (value)
                {
                    Importer_version = ImporterVersion.RetentionAnalysis;
                }
            }
        }

        [CategoryAttribute("Version"),
        DescriptionAttribute("analysing the isotope composition of metabolites accross different samples")]
        public bool version_IsotopeConcentrator
        {
            get { return Importer_version == ImporterVersion.IsotopeConcentrator; }
            set
            {
                if (value)
                {
                    Importer_version = ImporterVersion.IsotopeConcentrator;
                    wipeTrailingDecimals = true;
                }
            }
        }

        [CategoryAttribute("Version"),
        DescriptionAttribute("matching spectra against the Golm Metabolme Library (GMD) of reference spectra")]
        public bool version_LibrarySearch
        {
            get { return Importer_version == ImporterVersion.LibrarySearch; }
            set
            {
                if (value)
                {
                    Importer_version = ImporterVersion.LibrarySearch;
                    wipeTrailingDecimals = true;
                }
            }
        }
        #endregion version somethings

        #region Column settings
        [CategoryAttribute("Column settings"),
        DescriptionAttribute("the time in seconds bevor the first compound eludes from the column")]
        public float ColumnDeadTime { get; set; }

        [CategoryAttribute("Column settings"),
        DescriptionAttribute("the time lengthof the second dimension")]
        public float secondDimensionLength { get; set; }

        [CategoryAttribute("Column settings"),
        DescriptionAttribute("specifiing the number of wrap arounds on the swecond column for each analyte")]
        public WrapArounds wrapArounds { get; set; }
        #endregion Column settings

        #region Display
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public DisplayProperties exportFact { get; set; }
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public bool exportArea
        {
            get { return exportFact == DisplayProperties.area; }
            set
            {
                if (value)
                {
                    exportFact = DisplayProperties.area;
                }
            }
        }
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public bool firstDimensionRt
        {
            get { return exportFact == DisplayProperties.firstDimensionRt; }
            set
            {
                if (value)
                {
                    exportFact = DisplayProperties.firstDimensionRt;
                }
            }
        }
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public bool firstDimensionVanDenDoolRi
        {
            get { return exportFact == DisplayProperties.firstDimensionVanDenDoolRi; }
            set
            {
                if (value)
                {
                    exportFact = DisplayProperties.firstDimensionVanDenDoolRi;
                }
            }
        }
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public bool firstDimensionKovacRi
        {
            get { return exportFact == DisplayProperties.firstDimensionKovacRi; }
            set
            {
                if (value)
                {
                    exportFact = DisplayProperties.firstDimensionKovacRi;
                }
            }
        }
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public bool secondDimensionRt
        {
            get { return exportFact == DisplayProperties.secondDimensionRt; }
            set
            {
                if (value)
                {
                    exportFact = DisplayProperties.secondDimensionRt;
                }
            }
        }
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public bool secondDimensionVanDenDoolRi
        {
            get { return exportFact == DisplayProperties.secondDimensionVanDenDoolRi; }
            set
            {
                if (value)
                {
                    exportFact = DisplayProperties.secondDimensionVanDenDoolRi;
                }
            }
        }
        [CategoryAttribute("Display"),
        DescriptionAttribute("")]
        public bool secondDimensionReleativeAlkaneRt
        {
            get { return exportFact == DisplayProperties.secondDimensionReleativeAlkaneRt; }
            set
            {
                if (value)
                {
                    exportFact = DisplayProperties.secondDimensionReleativeAlkaneRt;
                }
            }
        }
        #endregion Display

        [CategoryAttribute("Misc"),
        DescriptionAttribute("")]
        [Browsable(false)]
        public MetMaxDataSet dataset { get; set; }

        #region ICloneable Members
        object ICloneable.Clone()
        {
            return new Options(this);
        }
        #endregion ICloneable Members

        #region IXmlSerializable Members
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return (null);
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            switch (reader.ReadContentAsString())
            {
                case "2": Importer_version = ImporterVersion.RetentionAnalysis;
                    break;
                case "3": Importer_version = ImporterVersion.IsotopeConcentrator;
                    break;
                case "4": Importer_version = ImporterVersion.LibrarySearch;
                    break;
                default:
                    throw new NotImplementedException();
            }

            wipeTrailingDecimals = reader.ReadElementContentAsBoolean();

            while (!reader.EOF)
            {
                wrapArounds.Add(new WrapAround(reader.ReadElementContentAsString(), reader.ReadElementContentAsInt()));
            }
        }  

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            //writer.WriteString(((short)Importer_version).ToString());
            writer.WriteValue(Importer_version);
            writer.WriteValue(wipeTrailingDecimals);
            foreach (WrapAround w in wrapArounds)
            {
                writer.WriteValue(w.name);
                writer.WriteValue(w.wraps);
            }
        }
        #endregion
    }
}
